﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using System.Collections;
using WHMS.EntityClasses;

namespace WMS
{
	public partial class SearchSupplyOrderForm : Form
	{
		public string SelectedNumber { get; set; }
		EntityField2 NumberField { get; set; }
		EntityField2 ProjectIdField { get; set; }
		EntityField2 DateField { get; set; }
		EntityRelation ProjectRelation { get; set; }
		RelationPredicateBucket Filter { get; set; }

		public SearchSupplyOrderForm(EntityField2 numberField, EntityField2 projectField, EntityField2 dateField, RelationPredicateBucket filter, Func<DataTable> projectsRetrievalFunc)
		{
			InitializeComponent();
			NumberField = numberField;
			ProjectIdField = projectField;
			DateField = dateField;
			//ProjectRelation = (EntityRelation)projectRelation;
			Filter = filter;

			var func = (projectsRetrievalFunc == null)? () => Business.GetRunningAndGeneralProjects() : projectsRetrievalFunc;
			FormsHelper.LoadComboBox(ProjectsDDL, ProjectFields.Id, ProjectFields.Name, func);
			FormsHelper.LoadComboBox(SupplierDDL, SupplierFields.Id, SupplierFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));
		}

		private void RunBtn_Click(object sender, EventArgs e)
		{
			var fields = new ResultsetFields(4);
			fields.DefineField(SupplyOrderFields.SupplyOrderNumber, 0, "Number");
			fields.DefineField(ProjectFields.Name, 1, "Project");
			fields.DefineField(SupplyOrderFields.Date, 2, "Date");
			fields.DefineField(SupplierFields.Name, 3, "Supplier");
			

			var filter = new RelationPredicateBucket();
			if (Filter != null)
			{
				//Filter = new RelationPredicateBucket();				
				filter.Relations.AddRange((ICollection)Filter.Relations);
				filter.PredicateExpression.Add(Filter.PredicateExpression);

			}
			//Filter.Relations.Add(ProjectRelation);

			if (SearchDateCB.Checked)
			{ 
				filter.PredicateExpression.Add(new FieldBetweenPredicate(DateField, null, DateFromDTP.Value, DateToDTP.Value));			
			}

			if (SearchNumberCB.Checked && !string.IsNullOrEmpty(NumberTB.Text))
			{
				var numberTerm = NumberTB.Text + "%";
				filter.PredicateExpression.Add(NumberField % numberTerm);
			}

			if(SearchByProjectCB.Checked)
			{
				filter.PredicateExpression.Add(ProjectIdField == GetSelectedProjectId());
			}

			if (SearchSupplierCB.Checked)
			{
				filter.PredicateExpression.Add(SupplyOrderFields.SupplierId == GetSelectedSupplierId());
			}

			filter.Relations.Add(SupplyOrderEntity.Relations.SupplierEntityUsingSupplierId);

			var dt = DALHelper.GetDynamicList(fields, filter, new SortExpression(NumberField | SortOperator.Ascending));
			dataGridView1.DataSource = dt;

			if (dt.Rows.Count > 0)
			{
				dataGridView1.Rows[0].Selected = true;
			}
		}

		private void OkBtn_Click(object sender, EventArgs e)
		{
			if (dataGridView1.SelectedRows.Count > 0)
			{
				SelectedNumber = (string)dataGridView1.SelectedRows[0].Cells[Number.Index].Value;
			}

			this.DialogResult = DialogResult.OK;

			this.Close();
		}

		private void SearchNumberCB_CheckedChanged(object sender, EventArgs e)
		{
			NumberTB.Enabled = SearchNumberCB.Checked;
		}

		private void SearchDateCB_CheckedChanged(object sender, EventArgs e)
		{
			DateFromDTP.Enabled = SearchDateCB.Checked;
			DateToDTP.Enabled = SearchDateCB.Checked;
		}

		private void SearchByProjectCB_CheckedChanged(object sender, EventArgs e)
		{
			ProjectsDDL.Enabled = SearchByProjectCB.Checked;
		}
		
		private int GetSelectedProjectId()
		{
			if (ProjectsDDL.SelectedItem == null)
				return 0;

			return (int)((System.Data.DataRowView)(ProjectsDDL.SelectedItem)).Row.ItemArray[0];
		}

		private int GetSelectedSupplierId()
		{
			if (SupplierDDL.SelectedItem == null)
				return 0;

			return (int)((System.Data.DataRowView)(SupplierDDL.SelectedItem)).Row.ItemArray[0];
		}

		private void SearchSupplierCB_CheckedChanged(object sender, EventArgs e)
		{
			SupplierDDL.Enabled = SearchSupplierCB.Checked;
		}


	}
}
